package base.thread_JUC.demo06;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 * 使用JUC实现：生产-》消费 轮询执行，直到规定时间停止.
 *
 * MYWHAT
 * 这是一个有问题的案例。 Netty 等消息队列设计里面对阻塞队列的典型应用实战
 *
 */
public class ProdConsumer_BlockQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        MyResource myResource = new MyResource(new ArrayBlockingQueue<>(10));

        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"\t 生产线程启动...");
            try {
                myResource.myProd();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"Prod").start();

        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"\t 消费线程启动...");
            try {
                myResource.myConsumer();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"Consumer").start();

        // 主线程让生产和消费线程运行5s，之后发出信号，停止生产+消费
        TimeUnit.SECONDS.sleep(5);

        System.out.println(Thread.currentThread().getName()+"\t 线程叫停，活动结束。");
        myResource.stop(false);
    }
}

/**
 * 线程共享资源类
 */
class MyResource {
    private volatile boolean flag = true;//开启生产+消费
    private AtomicInteger atomicInteger = new AtomicInteger();
    private BlockingQueue<String> blockingQueue=null;


    public MyResource(BlockingQueue<String> blockingQueue){
        this.blockingQueue = blockingQueue;
        System.out.println("本次实例化的阻塞队列是："+blockingQueue.getClass().getName());
    }

    public void stop(boolean flag){
        this.flag = flag;
    }

    public void myProd() throws Exception{
        String data = null;
        boolean offer = false;
        while (flag){
            data = atomicInteger.incrementAndGet()+"";
            offer = blockingQueue.offer(data, 1, TimeUnit.SECONDS);//1s只是为了控制台好看,当队列满了，1s还未消费，就放弃,接着下一轮生产
//            blockingQueue.put(data);
            if (offer) {
                System.out.println(Thread.currentThread().getName()+"\t 生产队列元素："+data+" 成功");
            }else {
                System.out.println(Thread.currentThread().getName()+"\t 生产队列元素："+data+" 失败");
            }
            TimeUnit.SECONDS.sleep(1);//为了控制台好看，1s生产一个
        }
        System.out.println(Thread.currentThread().getName()+"\t 收到 main 线程发送的信号 flag=false：停止往队列生产元素");
    }

    public void myConsumer() throws Exception{
        String poll = null;
        while (flag){// volatile flag 对修改时线程随时都可以感知，可见性
            poll = blockingQueue.poll(1, TimeUnit.SECONDS);// 1s都没有取到，就放弃
//            poll = blockingQueue.take();
            if (poll == null || "".equalsIgnoreCase(poll)) {
                System.out.println(Thread.currentThread().getName()+"\t 超过1s从队列中没有取到数据，消费退出");
                return;
            }
            System.out.println(Thread.currentThread().getName()+"\t 消费队列元素："+poll);
        }
    }
}


